package com.ninong.ker.workflow.rest;

import com.ninong.ker.admin.service.TaskService;
import com.ninong.ker.admin.service.dto.ActTaskDTO;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 任务
 * @author jmz
 */
@RestController
@RequestMapping("cmd/v1/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    /**
     * 获取我的代办、已办任务
     *
     * @return
     */
    @PostMapping
    public ResponseEntity<CmdResponse> list(@RequestBody ActTaskDTO dto, @RequestHeader(value = Constants.TOKEN) String token) {
        CmdResponse response = new CmdResponse();
        response.setData(taskService.listPage(dto, token));
        return ResponseEntity.ok(response);
    }


    /**
     *  根据instanceId查询任务审批记录
     *
     * @return
     */
    @GetMapping("/his/{instanceId}")
    public ResponseEntity<CmdResponse> listTaskHistorical(@PathVariable String instanceId) {
        CmdResponse response = new CmdResponse();
        response.setData(taskService.listTaskHistorical(instanceId));
        return ResponseEntity.ok(response);
    }

    /**
     * 审批任务
     *
     * @return
     */
    @PostMapping("/apply")
    public ResponseEntity<CmdResponse> applyTask(@RequestBody Map<String, Object> variables,@RequestHeader(value = Constants.TOKEN) String token) {
        taskService.applyTask(variables,token);
        return ResponseEntity.ok(new CmdResponse());
    }

    /**
     * 拒绝任务
     *
     * @return
     */
    @PostMapping("/reject")
    public ResponseEntity<CmdResponse> rejectTask(@RequestBody Map<String, Object> variables,@RequestHeader(value = Constants.TOKEN) String token) {
        taskService.rejectTask(variables,token);
        return ResponseEntity.ok(new CmdResponse());
    }
}
